﻿using FileSystem.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FileSystem.impl.Actions
{
    public class Print : ActionABC, IAction, INodeVisitor
    {
        private string fileName;
        private IResult lastResult;
        private IFSDirectory directory;

        //public Print()
        //    : base(new HelpDescription() { Name = "print", Description = "Prints given file." })
        //{
           
        //}

        public Print(string fileName): base(new HelpDescription() { Name = "print", Description = "Prints given file." })
        {
            this.fileName = fileName;
            
        }


        #region IAction Members

        public override IResult execute()
        {
            this.directory = AppResources.getInstance().getFileSystem().CurrentDirectory;
            IFSNode node = null;

            if (!string.IsNullOrEmpty(this.fileName))
            {
                node = directory.GetByName(this.fileName);
            }
            lastResult = null;

            if (node != null)
            {
                node.Accept(this);
            }
            else
            {
                lastResult = new Result(true, "Given file could not be found.");
            }

            return lastResult;
        }

        #endregion


        #region INodeVisitor Members

        public void VisitFile(IFSFile file)
        {
            lastResult = new Result(file.getContents());
        }

        public void VisitDirectory(IFSDirectory directory)
        {
            lastResult = new Result(true, "Given name is not a file");
        }

        public void VisitLink(IFSLink link)
        {
            if (link.GetLinkedNode() != null)
                link.GetLinkedNode().Accept(this);
        }

        #endregion

    }
}
